"""
File   : 01_1_chat_completions.py
Desc   : 对话补全
Date   : 2024/11/27
Author : Tianyu Chen
"""

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "system", 
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Who are you"
        }
    ]
)

print(completion.choices[0].message.content)
""" output:
I am an AI assistant created to help you with a variety of tasks, answer questions, provide information, and assist in problem-solving. If you have any questions or need assistance with something specific, feel free to ask!
"""

# print(completion)
""" output:
ChatCompletion(
    id='chatcmpl-AQ54YojzXa1R3amMyjORjatFrXgcy', 
    choices=[
        Choice(
            finish_reason='stop', 
            index=0, 
            logprobs=None, 
            message=ChatCompletionMessage(
                content='I am an AI language model created by OpenAI, designed to assist with a variety of questions and tasks by providing information and engaging in conversation. How can I help you today?', 
                refusal=None, 
                role='assistant', 
                audio=None, 
                function_call=None, 
                tool_calls=None
            )
        )
    ], 
    created=1730779390, 
    model='gpt-4o-mini-2024-07-18', 
    object='chat.completion', 
    service_tier=None, 
    system_fingerprint='fp_9b78b61c52', 
    usage=CompletionUsage(
        completion_tokens=36, 
        prompt_tokens=20, 
        total_tokens=56, 
        completion_tokens_details=CompletionTokensDetails(
            audio_tokens=None, 
            reasoning_tokens=0, 
            accepted_prediction_tokens=0, 
            rejected_prediction_tokens=0
        ), 
        prompt_tokens_details=PromptTokensDetails(
            audio_tokens=None, 
            cached_tokens=0
        )
    )
)
"""
